
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Django入门与实践-第11章：URL 分发 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="AdvancedConcept2.html" />
    
    
    <link rel="prev" href="Fundamentals-4.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="GettingStarted.html">
            
                <a href="GettingStarted.html">
            
                    
                    Django入门与实践-第1章：环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="GettingStarted-2.html">
            
                <a href="GettingStarted-2.html">
            
                    
                    Django入门与实践-第2章：创建项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="gettingstarted-3.html">
            
                <a href="gettingstarted-3.html">
            
                    
                    Django入门与实践-第3章：Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Fundamentals-1.html">
            
                <a href="Fundamentals-1.html">
            
                    
                    Django入门与实践-第4章：系统设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Fundamentals-2.html">
            
                <a href="Fundamentals-2.html">
            
                    
                    Django入门与实践-第5章：模型设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="Fundamentals-3-0.html">
            
                <a href="Fundamentals-3-0.html">
            
                    
                    Django入门与实践-第6章：第一个视图函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="Fundamentals-3-1.html">
            
                <a href="Fundamentals-3-1.html">
            
                    
                    Django入门与实践-第7章：模板引擎设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Fundamentals-3-1-1.html">
            
                <a href="Fundamentals-3-1-1.html">
            
                    
                    Django入门与实践-第8章：第一个单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="Fundamentals-3-2.html">
            
                <a href="Fundamentals-3-2.html">
            
                    
                    Django入门与实践-第9章：静态文件设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="Fundamentals-4.html">
            
                <a href="Fundamentals-4.html">
            
                    
                    Django入门与实践-第10章：Django Admin 介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.12" data-path="AdvancedConcepts.html">
            
                <a href="AdvancedConcepts.html">
            
                    
                    Django入门与实践-第11章：URL 分发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="AdvancedConcept2.html">
            
                <a href="AdvancedConcept2.html">
            
                    
                    Django入门与实践-第12章：复用模板
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="AdvancedConcept3.html">
            
                <a href="AdvancedConcept3.html">
            
                    
                    Django入门与实践-第13章：表单处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Authentication0.html">
            
                <a href="Authentication0.html">
            
                    
                    Django入门与实践-第14章：用户注册
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Authentication1.html">
            
                <a href="Authentication1.html">
            
                    
                    Django入门与实践-第15章：用户注销
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="Authentication2.html">
            
                <a href="Authentication2.html">
            
                    
                    Django入门与实践-第16章：用户登录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="DjangoORM0.html">
            
                <a href="DjangoORM0.html">
            
                    
                    Django入门与实践-第17章：保护视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="DjangoORM1.html">
            
                <a href="DjangoORM1.html">
            
                    
                    Django入门与实践-第18章：实现主题回复列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="DjangoORM2.html">
            
                <a href="DjangoORM2.html">
            
                    
                    Django入门与实践-第19章：实现主题回复功能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="DjangoORM3.html">
            
                <a href="DjangoORM3.html">
            
                    
                    Django入门与实践-第20章：查询结果集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="DjangoORM.html">
            
                <a href="DjangoORM.html">
            
                    
                    Django入门与实践-第21章：Django数据迁移
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="ClassBasedViews0.html">
            
                <a href="ClassBasedViews0.html">
            
                    
                    Django入门与实践-第22章：基于类的视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="ClassBasedViews1.html">
            
                <a href="ClassBasedViews1.html">
            
                    
                    Django入门与实践-第23章：分页实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="ClassBasedViews2.html">
            
                <a href="ClassBasedViews2.html">
            
                    
                    Django入门与实践-第24章：我的账户
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="ClassBasedViews3.html">
            
                <a href="ClassBasedViews3.html">
            
                    
                    Django入门与实践-第25章：Markdown支持
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="ClassBasedViews4.html">
            
                <a href="ClassBasedViews4.html">
            
                    
                    Django入门与实践-第26章：个性化设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="Deployment.html">
            
                <a href="Deployment.html">
            
                    
                    Django入门与实践-第27章：项目部署
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Django入门与实践-第11章：URL 分发</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="django&#x5165;&#x95E8;&#x4E0E;&#x5B9E;&#x8DF5;-&#x7B2C;11&#x7AE0;&#xFF1A;url-&#x5206;&#x53D1;">Django&#x5165;&#x95E8;&#x4E0E;&#x5B9E;&#x8DF5;-&#x7B2C;11&#x7AE0;&#xFF1A;URL &#x5206;&#x53D1;</h1>
<h3 id="&#x524D;&#x8A00;">&#x524D;&#x8A00;</h3>
<p>&#x5728;&#x672C;&#x8282;&#x8BFE;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x6DF1;&#x5165;&#x7406;&#x89E3;&#x4E24;&#x4E2A;&#x57FA;&#x672C;&#x6982;&#x5FF5;: URLs &#x548C; Forms&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x5C06;&#x5B66;&#x4E60;&#x5176;&#x5B83;&#x5F88;&#x591A;&#x6982;&#x5FF5;&#xFF0C;&#x5982;&#x521B;&#x5EFA;&#x53EF;&#x91CD;&#x7528;&#x6A21;&#x677F;&#x548C;&#x5B89;&#x88C5;&#x7B2C;&#x4E09;&#x65B9;&#x5E93;&#x3002;&#x540C;&#x65F6;&#x6211;&#x4EEC;&#x8FD8;&#x5C06;&#x7F16;&#x5199;&#x5927;&#x91CF;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x662F;&#x4ECE;&#x8FD9;&#x4E2A;&#x7CFB;&#x5217;&#x6559;&#x7A0B;&#x7684; part 1 &#x8DDF;&#x7740;&#x8FD9;&#x4E2A;&#x6559;&#x7A0B;&#x4E00;&#x6B65;&#x6B65;&#x5730;&#x7F16;&#x5199;&#x9879;&#x76EE;&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5728;&#x5F00;&#x59CB;&#x4E4B;&#x524D;&#x66F4;&#x65B0; <strong>models.py</strong>:</p>
<p><strong>boards/models.py</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Topic</span><span class="hljs-params">(models.Model)</span>:</span>
    <span class="hljs-comment"># other fields...</span>
    <span class="hljs-comment"># Add `auto_now_add=True` to the `last_updated` field</span>
    last_updated = models.DateTimeField(auto_now_add=<span class="hljs-keyword">True</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Post</span><span class="hljs-params">(models.Model)</span>:</span>
    <span class="hljs-comment"># other fields...</span>
    <span class="hljs-comment"># Add `null=True` to the `updated_by` field</span>
    updated_by = models.ForeignKey(User, null=<span class="hljs-keyword">True</span>, related_name=<span class="hljs-string">&apos;+&apos;</span>)
</code></pre>
<p>&#x73B0;&#x5728;&#x5728;&#x5DF2;&#x7ECF;&#x6FC0;&#x6D3B;&#x7684; virtualenv &#x73AF;&#x5883;&#x4E2D;&#x6267;&#x884C;&#x547D;&#x4EE4;&#xFF1A;</p>
<pre><code class="lang-shell">python manage.py makemigrations
python manage.py migrate
</code></pre>
<p>&#x5982;&#x679C;&#x5728;&#x4F60;&#x7684;&#x7A0B;&#x5E8F;&#x4E2D; <code>update_by</code> &#x5B57;&#x6BB5;&#x4E2D;&#x5DF2;&#x7ECF;&#x6709;&#x4E86; <code>null=True</code> &#x4E14; <code>last_updated</code> &#x5B57;&#x6BB5;&#x4E2D;&#x6709;&#x4E86; <code>auto_now_add=True</code>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x653E;&#x5FC3;&#x5730;&#x5FFD;&#x7565;&#x4E0A;&#x9762;&#x8FD9;&#x6B65;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x66F4;&#x559C;&#x6B22;&#x4F7F;&#x7528;&#x6211;&#x7684;&#x4EE3;&#x7801;&#x4F5C;&#x4E3A;&#x51FA;&#x53D1;&#x70B9;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728; GitHub &#x4E0A;&#x627E;&#x5230;&#x5B83;&#x3002;&#x672C;&#x9879;&#x76EE;&#x73B0;&#x5728;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x53EF;&#x4EE5;&#x5728; <strong>v0.2-lw</strong> &#x6807;&#x7B7E;&#x4E0B;&#x627E;&#x5230;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x94FE;&#x63A5;:[<a href="https://github.com/sibtc/django-beginners-guide/tree/v0.2-lw][1" target="_blank">https://github.com/sibtc/django-beginners-guide/tree/v0.2-lw][1</a>]</p>
<p>&#x6211;&#x4EEC;&#x7684;&#x5F00;&#x53D1;&#x5C31;&#x4ECE;&#x8FD9;&#x91CC;&#x5F00;&#x59CB;</p>
<h3 id="urls">URLs</h3>
<p>&#x968F;&#x7740;&#x6211;&#x4EEC;&#x9879;&#x76EE;&#x7684;&#x5F00;&#x53D1;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5C31;&#x662F;&#x5217;&#x51FA;&#x67D0;&#x4E2A;&#x677F;&#x5757;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x4E3B;&#x9898;&#x5217;&#x8868;&#xFF0C;&#x518D;&#x6765;&#x56DE;&#x987E;&#x4E00;&#x4E0B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4E0A;&#x4E00;&#x8282;&#x4E2D;&#x6211;&#x4EEC;&#x753B;&#x7684;&#x7EBF;&#x6846;&#x56FE;&#x3002;</p>
<p><img src="statics/3-2.png" alt="3-2.png"></p>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4ECE; <strong>myproject</strong> &#x76EE;&#x5F55;&#x4E2D;&#x7F16;&#x5199; <strong>urls.py</strong> &#x5F00;&#x59CB;&#xFF1A;</p>
<p><strong>myproject/urls.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin

<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^boards/(?P&lt;pk&gt;\d+)/$&apos;</span>, views.board_topics, name=<span class="hljs-string">&apos;board_topics&apos;</span>),
    url(<span class="hljs-string">r&apos;^admin/&apos;</span>, admin.site.urls),
]
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x82B1;&#x70B9;&#x65F6;&#x95F4;&#x6765;&#x5206;&#x6790;&#x4E00;&#x4E0B; <code>urlpatterns</code> &#x548C; <code>url</code>&#x3002;</p>
<p><strong>URL &#x8C03;&#x5EA6;&#x5668;&#xFF08;dispatcher&#xFF09;</strong> &#x548C; <strong>URLconf (URL configuration)</strong> &#x662F; Django &#x5E94;&#x7528;&#x4E2D;&#x7684;&#x57FA;&#x7840;&#x90E8;&#x5206;&#x3002;&#x5728;&#x5F00;&#x59CB;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD9;&#x4E2A;&#x770B;&#x8D77;&#x6765;&#x8BA9;&#x4EBA;&#x5F88;&#x56F0;&#x60D1;&#xFF1B;&#x6211;&#x8BB0;&#x5F97;&#x6211;&#x7B2C;&#x4E00;&#x6B21;&#x5F00;&#x59CB;&#x4F7F;&#x7528; Django &#x5F00;&#x53D1;&#x7684;&#x65F6;&#x5019;&#x4E5F;&#x6709;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x5B66;&#x8D77;&#x6765;&#x5F88;&#x56F0;&#x96BE;&#x3002;</p>
<p>&#x4E8B;&#x5B9E;&#x4E0A;&#xFF0C;Django&#x5F00;&#x53D1;&#x56E2;&#x961F;&#x6B63;&#x5728;&#x81F4;&#x529B;&#x4E8E;&#x5C06;&#x8DEF;&#x7531;&#x8BED;&#x6CD5;&#x7B80;&#x5316;&#xFF08;&#x8BD1;&#x6CE8;&#xFF1A;&#x5C31;&#x662F;&#x5C06;&#x539F;&#x6765;url&#x51FD;&#x6570;&#x66FF;&#x6362;&#x6210; path &#x51FD;&#x6570;&#xFF0C;&#x76EE;&#x524D;django2.0&#x5DF2;&#x7ECF;&#x6B63;&#x5F0F;&#x4F7F;&#x7528;&#x65B0;&#x7684;&#x8DEF;&#x7531;&#x8BED;&#x6CD5;&#xFF09;</p>
<p>&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#x53EF;&#x4EE5;&#x6709;&#x5F88;&#x591A; <strong>urls.py</strong> &#x5206;&#x5E03;&#x5728;&#x591A;&#x4E2A;&#x5E94;&#x7528;&#xFF08;app&#xFF09;&#x4E2D;&#x3002;Django &#x9700;&#x8981;&#x4E00;&#x4E2A; <strong>url.py</strong> &#x4F5C;&#x4E3A;&#x5165;&#x53E3;&#x3002;&#x8FD9;&#x4E2A;&#x7279;&#x6B8A;&#x7684; <strong>urls.py</strong> &#x53EB;&#x505A; <strong>&#x6839;&#x8DEF;&#x7531;&#x914D;&#x7F6E;&#xFF08;root URLconf&#xFF09;</strong>&#x3002;&#x5B83;&#x88AB;&#x5B9A;&#x4E49;&#x5728; <strong>settings.py</strong> &#x4E2D;&#x3002;</p>
<p><strong>myproject/settings.py</strong></p>
<pre><code class="lang-python">ROOT_URLCONF = <span class="hljs-string">&apos;myproject.urls&apos;</span>
</code></pre>
<p>&#x5B83;&#x5DF2;&#x7ECF;&#x81EA;&#x52A8;&#x914D;&#x7F6E;&#x597D;&#x4E86;&#xFF0C;&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x53BB;&#x6539;&#x53D8;&#x5B83;&#x4EFB;&#x4F55;&#x4E1C;&#x897F;&#x3002;</p>
<p>&#x5F53; Django &#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;(request)&#xFF0C; &#x5B83;&#x5C31;&#x4F1A;&#x5728;&#x9879;&#x76EE;&#x7684; URLconf &#x4E2D;&#x5BFB;&#x627E;&#x5339;&#x914D;&#x9879;&#x3002;&#x4ED6;&#x4ECE; <code>urlpatterns</code> &#x53D8;&#x91CF;&#x7684;&#x7B2C;&#x4E00;&#x6761;&#x5F00;&#x59CB;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x6BCF;&#x4E2A; <code>url</code> &#x4E2D;&#x53BB;&#x5339;&#x914D;&#x8BF7;&#x6C42;&#x7684; URL&#x3002;</p>
<p>&#x5982;&#x679C; Django &#x627E;&#x5230;&#x4E86;&#x4E00;&#x4E2A;&#x5339;&#x914D;&#x8DEF;&#x5F84;&#xFF0C;&#x4ED6;&#x4F1A;&#x628A;&#x8BF7;&#x6C42;(request)&#x53D1;&#x9001;&#x7ED9; <code>url</code> &#x7684;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570; <strong>&#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF08;view function&#xFF09;</strong>&#x3002;<code>urlpatterns</code> &#x4E2D;&#x7684;&#x987A;&#x5E8F;&#x5F88;&#x91CD;&#x8981;&#xFF0C;&#x56E0;&#x4E3A; Django &#x4E00;&#x65E6;&#x627E;&#x5230;&#x5339;&#x914D;&#x5C31;&#x4F1A;&#x505C;&#x6B62;&#x5F80;&#x540E;&#x641C;&#x7D22;&#x3002;&#x5982;&#x679C; Django &#x5728; URLconf &#x4E2D;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x5339;&#x914D;&#x9879;&#xFF0C;&#x4ED6;&#x4F1A;&#x901A;&#x8FC7; <strong>Page Not Found</strong> &#x7684;&#x9519;&#x8BEF;&#x5904;&#x7406;&#x4EE3;&#x7801;&#x629B;&#x51FA;&#x4E00;&#x4E2A; <strong>404</strong> &#x5F02;&#x5E38;&#x3002;</p>
<p>&#x8FD9;&#x662F; <code>url</code> &#x51FD;&#x6570;&#x7684;&#x5256;&#x6790;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">url</span><span class="hljs-params">(regex, view, kwargs=None, name=None)</span>:</span>
    <span class="hljs-comment"># ...</span>
</code></pre>
<ul>
<li><strong>regex</strong>&#xFF1A; &#x5339;&#x914D; URL patterns &#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x6CE8;&#x610F;&#xFF1A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x4F1A;&#x5FFD;&#x7565;&#x6389; <strong>GET</strong> &#x6216;&#x8005; <strong>POST</strong> &#x540E;&#x9762;&#x7684;&#x53C2;&#x6570;&#x3002;&#x5728;&#x4E00;&#x4E2A; <strong><a href="http://127.0.0.1:8000/boards/?page=2" target="_blank">http://127.0.0.1:8000/boards/?page=2</a></strong> &#x7684;&#x8BF7;&#x6C42;&#x4E2D;&#xFF0C;&#x53EA;&#x6709; <strong>/boards/</strong> &#x4F1A;&#x88AB;&#x5904;&#x7406;&#x3002;</li>
<li><strong>view</strong>&#xFF1A; &#x89C6;&#x56FE;&#x51FD;&#x6570;&#x88AB;&#x7528;&#x6765;&#x5904;&#x7406;&#x7528;&#x6237;&#x8BF7;&#x6C42;&#xFF0C;&#x540C;&#x65F6;&#x5B83;&#x8FD8;&#x53EF;&#x4EE5;&#x662F; <strong>django.conf.urls.include</strong> &#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5B83;&#x5C06;&#x5F15;&#x7528;&#x4E00;&#x4E2A;&#x5916;&#x90E8;&#x7684;<strong>urls.py</strong>&#x6587;&#x4EF6;&#xFF0C;&#x4F8B;&#x5982;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x6765;&#x5B9A;&#x4E49;&#x4E00;&#x7EC4;&#x7279;&#x5B9A;&#x4E8E;&#x5E94;&#x7528;&#x7684; URLs&#xFF0C;&#x4F7F;&#x7528;&#x524D;&#x7F00;&#x5C06;&#x5176;&#x5305;&#x542B;&#x5728;&#x6839; URLconf &#x4E2D;&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x5728;&#x540E;&#x9762;&#x7EE7;&#x7EED;&#x63A2;&#x8BA8;&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#x3002;</li>
<li><strong>kwargs</strong>&#xFF1A;&#x4F20;&#x9012;&#x7ED9;&#x76EE;&#x6807;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x4EFB;&#x610F;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#xFF0C;&#x5B83;&#x901A;&#x5E38;&#x7528;&#x4E8E;&#x5728;&#x53EF;&#x91CD;&#x7528;&#x89C6;&#x56FE;&#x4E0A;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x7B80;&#x5355;&#x7684;&#x5B9A;&#x5236;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x662F;&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#x5B83;&#x3002;</li>
<li><strong>name:</strong>&#xFF1A; &#x8BE5; URL &#x7684;&#x552F;&#x4E00;&#x6807;&#x8BC6;&#x7B26;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x7684;&#x7279;&#x5F81;&#x3002;&#x8981;&#x59CB;&#x7EC8;&#x8BB0;&#x5F97;&#x4E3A;&#x4F60;&#x7684; URLs &#x547D;&#x540D;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x5F88;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x70B9;&#x662F;&#xFF1A;&#x4E0D;&#x8981;&#x5728; views(&#x89C6;&#x56FE;) &#x6216;&#x8005; templates(&#x6A21;&#x677F;) &#x4E2D;&#x786C;&#x7F16;&#x7801; URL&#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7;&#x5B83;&#x7684;&#x540D;&#x5B57;&#x53BB;&#x5F15;&#x7528; URL&#x3002;</li>
</ul>
<p><img src="statics/3-3.png" alt="3-3.png"></p>
<h3 id="&#x57FA;&#x7840;-urls-&#x8DEF;&#x7531;">&#x57FA;&#x7840; URLs &#x8DEF;&#x7531;</h3>
<p>&#x57FA;&#x7840;URL&#x521B;&#x5EFA;&#x8D77;&#x6765;&#x5F88;&#x5BB9;&#x6613;&#x3002;&#x5C31;&#x53EA;&#x662F;&#x4E2A;&#x5339;&#x914D;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x95EE;&#x9898;&#x3002;&#x6BD4;&#x5982;&#x8BF4;&#xFF0C;&#x6211;&#x4EEC;&#x60F3;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; &quot;about&quot; &#x9875;&#x9762;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^about/$&apos;</span>, views.about, name=<span class="hljs-string">&apos;about&apos;</span>),
]
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x66F4;&#x6DF1;&#x5C42;&#x4E00;&#x70B9;&#x7684; URL &#x7ED3;&#x6784;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^about/$&apos;</span>, views.about, name=<span class="hljs-string">&apos;about&apos;</span>),
    url(<span class="hljs-string">r&apos;^about/company/$&apos;</span>, views.about_company, name=<span class="hljs-string">&apos;about_company&apos;</span>),
    url(<span class="hljs-string">r&apos;^about/author/$&apos;</span>, views.about_author, name=<span class="hljs-string">&apos;about_author&apos;</span>),
    url(<span class="hljs-string">r&apos;^about/author/vitor/$&apos;</span>, views.about_vitor, name=<span class="hljs-string">&apos;about_vitor&apos;</span>),
    url(<span class="hljs-string">r&apos;^about/author/erica/$&apos;</span>, views.about_erica, name=<span class="hljs-string">&apos;about_erica&apos;</span>),
    url(<span class="hljs-string">r&apos;^privacy/$&apos;</span>, views.privacy_policy, name=<span class="hljs-string">&apos;privacy_policy&apos;</span>),
]
</code></pre>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E9B;&#x7B80;&#x5355;&#x7684; URL &#x8DEF;&#x7531;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5BF9;&#x4E8E;&#x4E0A;&#x9762;&#x6240;&#x6709;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x7ED3;&#x6784;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">about</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># do something...</span>
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;about.html&apos;</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">about_company</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># do something else...</span>
    <span class="hljs-comment"># return some data along with the view...</span>
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;about_company.html&apos;</span>, {<span class="hljs-string">&apos;company_name&apos;</span>: <span class="hljs-string">&apos;Simple Complex&apos;</span>})
</code></pre>
<h3 id="&#x9AD8;&#x7EA7;-urls-&#x8DEF;&#x7531;">&#x9AD8;&#x7EA7; URLs &#x8DEF;&#x7531;</h3>
<p>&#x66F4;&#x9AD8;&#x7EA7;&#x7684;URL&#x8DEF;&#x7531;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;&#x662F;&#x901A;&#x8FC7;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x6765;&#x5339;&#x914D;&#x67D0;&#x4E9B;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x5E76;&#x521B;&#x5EFA;&#x52A8;&#x6001; URL</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x4E2A;&#x4EBA;&#x8D44;&#x6599;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x8BF8;&#x5982; github.com/vitorfs or twitter.com/vitorfs(vitorfs &#x662F;&#x6211;&#x7684;&#x7528;&#x6237;&#x540D;) &#x8FD9;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x50CF;&#x4EE5;&#x4E0B;&#x51E0;&#x70B9;&#x8FD9;&#x6837;&#x505A;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^(?P&lt;username&gt;[\w.@+-]+)/$&apos;</span>, views.user_profile, name=<span class="hljs-string">&apos;user_profile&apos;</span>),
]
</code></pre>
<p>&#x5B83;&#x4F1A;&#x5339;&#x914D; Django &#x7528;&#x6237;&#x6A21;&#x578B;&#x91CC;&#x9762;&#x6240;&#x6709;&#x6709;&#x6548;&#x7684;&#x7528;&#x6237;&#x540D;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x662F;&#x4E00;&#x4E2A;&#x5F88;&#x5BBD;&#x677E;&#x7684; URL&#x3002;&#x8FD9;&#x610F;&#x5473;&#x5927;&#x91CF;&#x7684; URL patterns &#x90FD;&#x4F1A;&#x88AB;&#x5B83;&#x5339;&#x914D;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5B9A;&#x4E49;&#x5728; URL &#x7684;&#x6839;&#xFF0C;&#x800C;&#x4E0D;&#x50CF; <strong>/profile/<username>/</username></strong> &#x8FD9;&#x6837;&#x3002;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x60F3;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A; <strong>/about/</strong> &#x7684;URL&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x628A;&#x5B83;&#x5B9A;&#x4E49;&#x5728;&#x8FD9;&#x4E2A; username URL pattern &#x7684;&#x524D;&#x9762;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^about/$&apos;</span>, views.about, name=<span class="hljs-string">&apos;about&apos;</span>),
    url(<span class="hljs-string">r&apos;^(?P&lt;username&gt;[\w.@+-]+)/$&apos;</span>, views.user_profile, name=<span class="hljs-string">&apos;user_profile&apos;</span>),
]
</code></pre>
<p>&#x5982;&#x679C;&#x8FD9;&#x4E2A; &quot;about&quot; &#x9875;&#x9762;&#x5B9A;&#x4E49;&#x5728;u srename URL pattern &#x540E;&#x9762;&#xFF0C;Django &#x5C06;&#x6C38;&#x8FDC;&#x627E;&#x4E0D;&#x5230;&#x5B83;&#xFF0C;&#x56E0;&#x4E3A; &quot;about&quot; &#x8FD9;&#x4E2A;&#x5355;&#x8BCD;&#x4F1A;&#x5148;&#x88AB;usrname&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x6240;&#x5339;&#x914D;&#x5230;&#xFF0C;&#x89C6;&#x56FE;&#x51FD;&#x6570; <code>user_profile</code> &#x5C06;&#x4F1A;&#x88AB;&#x6267;&#x884C;&#x800C;&#x4E0D;&#x662F;&#x6267;&#x884C; <code>about</code>&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;&#x8FD9;&#x6709;&#x4E00;&#x4E9B;&#x526F;&#x4F5C;&#x7528;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4ECE;&#x73B0;&#x5728;&#x5F00;&#x59CB;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x628A; &quot;about&quot; &#x89C6;&#x4E3A;&#x7981;&#x6B62;&#x4F7F;&#x7528;&#x7684;username&#xFF0C;&#x56E0;&#x4E3A;&#x5982;&#x679C;&#x6709;&#x7528;&#x6237;&#x5C06; &quot;about&quot; &#x4F5C;&#x4E3A;&#x4ED6;&#x4EEC;&#x7684;username&#xFF0C;&#x4ED6;&#x4EEC;&#x5C06;&#x6C38;&#x8FDC;&#x4E0D;&#x80FD;&#x770B;&#x5230;&#x4ED6;&#x4EEC;&#x7684;&#x4E2A;&#x4EBA;&#x8D44;&#x6599;&#x9875;&#x9762;&#xFF0C;&#x800C;&#x770B;&#x5230;&#x7684;about&#x9875;&#x9762;&#x3002;</p>
<p><img src="statics/3-4.png" alt="3-4.png"></p>
<blockquote>
<p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x7ED9;&#x7528;&#x6237;&#x4E2A;&#x4EBA;&#x4E3B;&#x9875;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x5F88;&#x9177;&#x7684;&#x4E3B;&#x9875;&#x7684;URL&#xFF0C;&#x90A3;&#x4E48;&#x907F;&#x514D;&#x51B2;&#x7A81;&#x6700;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x524D;&#x7F00;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;/u/vitorfs&#xFF0C;&#x6216;&#x8005;&#x50CF; Medium &#x4E00;&#x6837;&#x4F7F;&#x7528; @ &#x4F5C;&#x4E3A;&#x524D;&#x7F00; /@vitorfs/&#x3002;</p>
</blockquote>
<p>&#x8FD9;&#x4E9B; URL &#x8DEF;&#x7531;&#x7684;&#x4E3B;&#x8981;&#x601D;&#x60F3;&#x662F;&#x5F53; URL &#x7684;&#x4E00;&#x90E8;&#x5206;&#x88AB;&#x5F53;&#x4F5C;&#x67D0;&#x4E9B;&#x8D44;&#x6E90;(&#x8FD9;&#x4E9B;&#x8D44;&#x6E90;&#x7528;&#x6765;&#x6784;&#x6210;&#x67D0;&#x4E2A;&#x9875;&#x9762;)&#x7684;&#x6807;&#x8BC6;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x53BB;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x52A8;&#x6001;&#x9875;&#x9762;&#x3002;&#x6BD4;&#x5982;&#x8BF4;&#xFF0C;&#x8FD9;&#x4E2A;&#x6807;&#x8BC6;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#x7684; ID &#x6216;&#x8005;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<p>&#x5F00;&#x59CB;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <strong>Board</strong> ID &#x53BB;&#x521B;&#x5EFA; <strong>Topics</strong>&#x5217;&#x8868;&#x7684;&#x52A8;&#x6001;&#x9875;&#x9762;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x518D;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x6211;&#x5728; <strong>URLs</strong> &#x5F00;&#x5934;&#x7684;&#x90E8;&#x5206;&#x7ED9;&#x51FA;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-python">url(<span class="hljs-string">r&apos;^boards/(?P&lt;pk&gt;\d+)/$&apos;</span>, views.board_topics, name=<span class="hljs-string">&apos;board_topics&apos;</span>)
</code></pre>
<p>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x7684; <code>\d+</code> &#x4F1A;&#x5339;&#x914D;&#x4E00;&#x4E2A;&#x4EFB;&#x610F;&#x5927;&#x5C0F;&#x7684;&#x6574;&#x6570;&#x503C;&#x3002;&#x8FD9;&#x4E2A;&#x6574;&#x6570;&#x503C;&#x7528;&#x6765;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x53D6;&#x5230; &#x6307;&#x5B9A;&#x7684; <strong>Board</strong>&#x3002;&#x73B0;&#x5728;&#x6CE8;&#x610F;&#x6211;&#x4EEC;&#x8FD9;&#x6837;&#x5199;&#x8FD9;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F; <code>(?P&lt;pk&gt;\d+)</code>&#xFF0C;&#x8FD9;&#x662F;&#x544A;&#x8BC9; Django &#x5C06;&#x6355;&#x83B7;&#x5230;&#x7684;&#x503C;&#x653E;&#x5165;&#x540D;&#x4E3A; <strong>pk</strong> &#x7684;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x4E2D;&#x3002;</p>
<p>&#x8FD9;&#x65F6;&#x6211;&#x4EEC;&#x4E3A;&#x5B83;&#x5199;&#x7684;&#x4E00;&#x4E2A;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">board_topics</span><span class="hljs-params">(request, pk)</span>:</span>
    <span class="hljs-comment"># do something...</span>
</code></pre>
<p>&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86; <code>(?P&lt;pk&gt;\d+)</code> &#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x5728; <code>board_topics</code>&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x5FC5;&#x987B;&#x547D;&#x540D;&#x4E3A; <strong>pk</strong>&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x5728;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4F7F;&#x7528;&#x4EFB;&#x610F;&#x540D;&#x5B57;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-python">url(<span class="hljs-string">r&apos;^boards/(\d+)/$&apos;</span>, views.board_topics, name=<span class="hljs-string">&apos;board_topics&apos;</span>)
</code></pre>
<p>&#x7136;&#x540E;&#x5728;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5B9A;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">board_topics</span><span class="hljs-params">(request, board_id)</span>:</span>
    <span class="hljs-comment"># do something...</span>
</code></pre>
<p>&#x6216;&#x8005;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">board_topics</span><span class="hljs-params">(request, id)</span>:</span>
    <span class="hljs-comment"># do something...</span>
</code></pre>
<p>&#x540D;&#x5B57;&#x65E0;&#x5173;&#x7D27;&#x8981;&#xFF0C;&#x4F46;&#x662F;&#x4F7F;&#x7528;&#x547D;&#x540D;&#x53C2;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x5F88;&#x597D;&#x7684;&#x505A;&#x6CD5;&#xFF0C;&#x56E0;&#x4E3A;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x6709;&#x4E2A;&#x66F4;&#x5927;&#x7684;URL&#x53BB;&#x6355;&#x83B7;&#x591A;&#x4E2A; ID &#x548C;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x8FD9;&#x4F1A;&#x66F4;&#x4FBF;&#x4E8E;&#x6211;&#x4EEC;&#x9605;&#x8BFB;&#x3002;</p>
<blockquote>
<p>PK or ID&#xFF1F;
PK &#x8868;&#x793A;&#x4E3B;&#x952E;&#xFF08;Primary key&#xFF09;&#xFF0C;&#x8FD9;&#x662F;&#x8BBF;&#x95EE;&#x6A21;&#x578B;&#x7684;&#x4E3B;&#x952E;ID&#x7684;&#x7B80;&#x5199;&#x65B9;&#x6CD5;&#xFF0C;&#x6240;&#x6709;Django&#x6A21;&#x578B;&#x90FD;&#x6709;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x66F4;&#x591A;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F7F;&#x7528;pk&#x5C5E;&#x6027;&#x548C;&#x4F7F;&#x7528;id&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x7ED9;model&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x65F6;&#xFF0C;Django&#x5C06;&#x81EA;&#x52A8;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; AutoField &#x7C7B;&#x578B;&#x7684;&#x5B57;&#x6BB5;&#xFF0C;&#x540D;&#x5B57;&#x53EB;&#x505A; id&#xFF0C;&#x5B83;&#x5C31;&#x662F;&#x4E3B;&#x952E;&#x3002;<br>&#x5982;&#x679C;&#x4F60;&#x7ED9;model&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x4E3B;&#x952E;&#xFF0C;&#x4F8B;&#x5982;&#xFF0C;&#x5047;&#x8BBE; email &#x662F;&#x4F60;&#x7684;&#x4E3B;&#x952E;&#xFF0C;&#x4F60;&#x5C31;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x8BBF;&#x95EE;&#xFF1A;obj.email &#x6216;&#x8005; obj.pk&#xFF0C;&#x4E8C;&#x8005;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#x3002;</p>
</blockquote>
<h3 id="&#x4F7F;&#x7528;-urls-api">&#x4F7F;&#x7528; URLs API</h3>
<p>&#x73B0;&#x5728;&#x5230;&#x4E86;&#x5199;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#x4E86;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x5B9E;&#x73B0;&#x6211;&#x5728;&#x5F00;&#x5934;&#x63D0;&#x5230;&#x7684;&#x4E3B;&#x9898;&#x5217;&#x8868;&#x9875;&#x9762;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x7F16;&#x8F91; <strong>urls.py</strong>&#xFF0C; &#x6DFB;&#x52A0;&#x65B0;&#x7684; URL &#x8DEF;&#x7531;</p>
<p><strong>myproject/urls.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin

<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^boards/(?P&lt;pk&gt;\d+)/$&apos;</span>, views.board_topics, name=<span class="hljs-string">&apos;board_topics&apos;</span>),
    url(<span class="hljs-string">r&apos;^admin/&apos;</span>, admin.site.urls),
]
</code></pre>
<p>&#x73B0;&#x5728;&#x521B;&#x5EFA;&#x89C6;&#x56FE;&#x51FD;&#x6570; <code>board_topics</code>&#xFF1A;</p>
<p><strong>boards/views.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># code suppressed for brevity</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">board_topics</span><span class="hljs-params">(request, pk)</span>:</span>
    board = Board.objects.get(pk=pk)
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;topics.html&apos;</span>, {<span class="hljs-string">&apos;board&apos;</span>: board})
</code></pre>
<p>&#x5728; <strong>templates</strong> &#x76EE;&#x5F55;&#x4E2D;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <strong>topics.html</strong> &#x7684;&#x6A21;&#x677F;&#xFF1A;</p>
<p><strong>templates/topics.html</strong></p>
<pre><code class="lang-html">{% load static %}<span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% static &apos;css/bootstrap.min.css&apos; %}&quot;</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ol</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb my-4&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span>Boards<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x53EA;&#x662F;&#x521B;&#x5EFA;&#x65B0;&#x7684; HTML &#x6A21;&#x677F;&#x3002;&#x4E0D;&#x7528;&#x62C5;&#x5FC3;&#xFF0C;&#x5728;&#x4E0B;&#x4E00;&#x8282;&#x4E2D;&#x6211;&#x4F1A;&#x5411;&#x4F60;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x521B;&#x5EFA;&#x53EF;&#x91CD;&#x7528;&#x6A21;&#x677F;&#x3002;</p>
</blockquote>
<p>&#x73B0;&#x5728;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x6253;&#x5F00; URL <strong><a href="http://127.0.0.1:8000/boards/1/" target="_blank">http://127.0.0.1:8000/boards/1/</a></strong> &#xFF0C;&#x7ED3;&#x679C;&#x5E94;&#x8BE5;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x9875;&#x9762;&#xFF1A;</p>
<p><img src="statics/3-5.png" alt="3-5.png"></p>
<p>&#x73B0;&#x5728;&#x5230;&#x4E86;&#x5199;&#x4E00;&#x4E9B;&#x6D4B;&#x8BD5;&#x7684;&#x65F6;&#x5019;&#x4E86;&#xFF01;&#x7F16;&#x8F91; <strong>test.py</strong>&#xFF0C;&#x5728;&#x6587;&#x4EF6;&#x5E95;&#x90E8;&#x6DFB;&#x52A0;&#x4E0B;&#x9762;&#x7684;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<p><strong>boards/tests.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.core.urlresolvers <span class="hljs-keyword">import</span> reverse
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> resolve
<span class="hljs-keyword">from</span> django.test <span class="hljs-keyword">import</span> TestCase
<span class="hljs-keyword">from</span> .views <span class="hljs-keyword">import</span> home, board_topics
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HomeTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-comment"># ...</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BoardTopicsTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        Board.objects.create(name=<span class="hljs-string">&apos;Django&apos;</span>, description=<span class="hljs-string">&apos;Django board.&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_board_topics_view_success_status_code</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;board_topics&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.get(url)
        self.assertEquals(response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_board_topics_view_not_found_status_code</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;board_topics&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">99</span>})
        response = self.client.get(url)
        self.assertEquals(response.status_code, <span class="hljs-number">404</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_board_topics_url_resolves_board_topics_view</span><span class="hljs-params">(self)</span>:</span>
        view = resolve(<span class="hljs-string">&apos;/boards/1/&apos;</span>)
        self.assertEquals(view.func, board_topics)
</code></pre>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x51E0;&#x4EF6;&#x4E8B;&#x60C5;&#x3002;&#x8FD9;&#x6B21;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86; <code>setUp</code> &#x65B9;&#x6CD5;&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A; <strong>Board</strong> &#x5B9E;&#x4F8B;&#x6765;&#x7528;&#x4E8E;&#x6D4B;&#x8BD5;&#x3002;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x8FD9;&#x6837;&#x505A;&#xFF0C;&#x56E0;&#x4E3A; Django &#x7684;&#x6D4B;&#x8BD5;&#x673A;&#x5236;&#x4E0D;&#x4F1A;&#x9488;&#x5BF9;&#x5F53;&#x524D;&#x6570;&#x636E;&#x5E93;&#x8DD1;&#x4F60;&#x7684;&#x6D4B;&#x8BD5;&#x3002;&#x8FD0;&#x884C; Django &#x6D4B;&#x8BD5;&#x65F6;&#x4F1A;&#x5373;&#x65F6;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6570;&#x636E;&#x5E93;&#xFF0C;&#x5E94;&#x7528;&#x6240;&#x6709;&#x7684;model(&#x6A21;&#x578B;)&#x8FC1;&#x79FB; &#xFF0C;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x5B8C;&#x6210;&#x540E;&#x4F1A;&#x9500;&#x6BC1;&#x8FD9;&#x4E2A;&#x7528;&#x4E8E;&#x6D4B;&#x8BD5;&#x7684;&#x6570;&#x636E;&#x5E93;&#x3002;</p>
<p>&#x56E0;&#x6B64;&#x5728; <code>setUp</code> &#x65B9;&#x6CD5;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x51C6;&#x5907;&#x4E86;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x7684;&#x73AF;&#x5883;&#xFF0C;&#x7528;&#x6765;&#x6A21;&#x62DF;&#x573A;&#x666F;&#x3002;</p>
<ul>
<li><code>test_board_topics_view_success_status_code</code> &#x65B9;&#x6CD5;&#xFF1A;&#x6D4B;&#x8BD5; Django &#x662F;&#x5426;&#x5BF9;&#x4E8E;&#x73B0;&#x6709;&#x7684; <strong>Board</strong> &#x8FD4;&#x56DE; status code(&#x72B6;&#x6001;&#x7801;) 200(&#x6210;&#x529F;)&#x3002;</li>
<li><code>test_board_topics_view_not_found_status_code</code> &#x65B9;&#x6CD5;&#xFF1A;&#x6D4B;&#x8BD5; Django &#x662F;&#x5426;&#x5BF9;&#x4E8E;&#x4E0D;&#x5B58;&#x5728;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x7684; <strong>Board</strong> &#x8FD4;&#x56DE; status code 404(&#x9875;&#x9762;&#x672A;&#x627E;&#x5230;)&#x3002;</li>
<li><code>test_board_topics_url_resolves_board_topics_view</code> &#x65B9;&#x6CD5;&#xFF1A;&#x6D4B;&#x8BD5; Django &#x662F;&#x5426;&#x4F7F;&#x7528;&#x4E86;&#x6B63;&#x786E;&#x7684;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x53BB;&#x6E32;&#x67D3; topics&#x3002;</li>
</ul>
<p>&#x73B0;&#x5728;&#x6765;&#x8FD0;&#x884C;&#x4E00;&#x4E0B;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<p>&#x8F93;&#x51FA;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
.E...
======================================================================
ERROR: test_board_topics_view_not_found_status_code (boards.tests.BoardTopicsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
# ...
boards.models.DoesNotExist: Board matching query does not exist.

----------------------------------------------------------------------
Ran 5 tests in 0.093s

FAILED (errors=1)
Destroying test database for alias &apos;default&apos;...
</code></pre>
<p>&#x6D4B;&#x8BD5; <strong>test_board_topics_view_not_found_status_code</strong> &#x5931;&#x8D25;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728; Traceback &#x4E2D;&#x770B;&#x5230;&#x8FD4;&#x56DE;&#x4E86;&#x4E00;&#x4E2A; exception(&#x5F02;&#x5E38;) &#x201C;boards.models.DoesNotExist: Board matching query does not exist.&#x201D;</p>
<p><img src="statics/3-6.png" alt="3-6.png"></p>
<p>&#x5728; <code>DEBUG=False</code> &#x7684;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#x4E2D;&#xFF0C;&#x8BBF;&#x95EE;&#x8005;&#x4F1A;&#x770B;&#x5230;&#x4E00;&#x4E2A; <strong>500 Internal Server Error</strong> &#x7684;&#x9875;&#x9762;&#x3002;&#x4F46;&#x662F;&#x8FD9;&#x4E0D;&#x662F;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5F97;&#x5230;&#x7684;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x4E00;&#x4E2A; <strong>404 Page Not Found</strong> &#x7684;&#x9875;&#x9762;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x91CD;&#x5199;&#x6211;&#x4EEC;&#x7684;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x3002;</p>
<p><strong>boards/views.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render
<span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> Http404
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># code suppressed for brevity</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">board_topics</span><span class="hljs-params">(request, pk)</span>:</span>
    <span class="hljs-keyword">try</span>:
        board = Board.objects.get(pk=pk)
    <span class="hljs-keyword">except</span> Board.DoesNotExist:
        <span class="hljs-keyword">raise</span> Http404
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;topics.html&apos;</span>, {<span class="hljs-string">&apos;board&apos;</span>: board})
</code></pre>
<p>&#x91CD;&#x65B0;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">python manage.py test
</code></pre>
<pre><code class="lang-python">Creating test database <span class="hljs-keyword">for</span> alias <span class="hljs-string">&apos;default&apos;</span>...
System check identified no issues (<span class="hljs-number">0</span> silenced).
.....
----------------------------------------------------------------------
Ran <span class="hljs-number">5</span> tests <span class="hljs-keyword">in</span> <span class="hljs-number">0.042</span>s

OK
Destroying test database <span class="hljs-keyword">for</span> alias <span class="hljs-string">&apos;default&apos;</span>...
</code></pre>
<p>&#x597D;&#x6781;&#x4E86;&#xFF01;&#x73B0;&#x5728;&#x5B83;&#x6309;&#x7167;&#x9884;&#x671F;&#x5DE5;&#x4F5C;&#x3002;</p>
<p><img src="statics/3-7.png" alt="3-7.png"></p>
<p>&#x8FD9;&#x662F; Django &#x5728; <code>DEBUG=False</code> &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x663E;&#x793A;&#x7684;&#x9ED8;&#x8BA4;&#x9875;&#x9762;&#x3002;&#x7A0D;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x81EA;&#x5B9A;&#x4E49; 404 &#x9875;&#x9762;&#x53BB;&#x663E;&#x793A;&#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x7684;&#x4E1C;&#x897F;&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5E38;&#x89C1;&#x7684;&#x7528;&#x6CD5;&#x3002;&#x4E8B;&#x5B9E;&#x4E0A;&#xFF0C; Django &#x6709;&#x4E00;&#x4E2A;&#x5FEB;&#x6377;&#x65B9;&#x5F0F;&#x53BB;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x6216;&#x8005;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x5BF9;&#x8C61; 404&#x3002;</p>
<p>&#x56E0;&#x6B64;&#x8BA9;&#x6211;&#x4EEC;&#x518D;&#x6765;&#x91CD;&#x5199;&#x4E00;&#x4E0B; <strong>board_topics</strong> &#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, get_object_or_404
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-comment"># code suppressed for brevity</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">board_topics</span><span class="hljs-params">(request, pk)</span>:</span>
    board = get_object_or_404(Board, pk=pk)
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;topics.html&apos;</span>, {<span class="hljs-string">&apos;board&apos;</span>: board})
</code></pre>
<p>&#x4FEE;&#x6539;&#x4E86;&#x4EE3;&#x7801;&#xFF0C;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#x3002;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
.....
----------------------------------------------------------------------
Ran 5 tests in 0.052s

OK
Destroying test database for alias &apos;default&apos;...
</code></pre>
<p>&#x6CA1;&#x6709;&#x7834;&#x574F;&#x4EFB;&#x4F55;&#x4E1C;&#x897F;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x6211;&#x4EEC;&#x7684;&#x5F00;&#x53D1;&#x3002;</p>
<p>&#x4E0B;&#x4E00;&#x6B65;&#x662F;&#x5728;&#x5C4F;&#x5E55;&#x4E0A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5BFC;&#x822A;&#x94FE;&#x63A5;&#x3002;&#x4E3B;&#x9875;&#x5E94;&#x8BE5;&#x6709;&#x4E00;&#x4E2A;&#x94FE;&#x63A5;&#x6307;&#x5F15;&#x8BBF;&#x95EE;&#x8005;&#x53BB;&#x8BBF;&#x95EE;&#x6307;&#x5B9A;&#x677F;&#x5757;&#x4E0B;&#x9762;&#x7684;&#x4E3B;&#x9898;&#x5217;&#x8868;&#x9875;&#x9762;&#x3002;&#x540C;&#x6837;&#x5730;&#xFF0C;topics &#x9875;&#x9762;&#x4E5F;&#x5E94;&#x5F53;&#x6709;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x4E3B;&#x9875;&#x7684;&#x94FE;&#x63A5;&#x3002;</p>
<p><img src="statics/3-8.png" alt="3-8.png"></p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5148;&#x4E3A; <code>HomeTests</code> &#x7C7B;&#x7F16;&#x5199;&#x4E00;&#x4E9B;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<p><strong>boards/test.py</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HomeTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        self.board = Board.objects.create(name=<span class="hljs-string">&apos;Django&apos;</span>, description=<span class="hljs-string">&apos;Django board.&apos;</span>)
        url = reverse(<span class="hljs-string">&apos;home&apos;</span>)
        self.response = self.client.get(url)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_home_view_status_code</span><span class="hljs-params">(self)</span>:</span>
        self.assertEquals(self.response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_home_url_resolves_home_view</span><span class="hljs-params">(self)</span>:</span>
        view = resolve(<span class="hljs-string">&apos;/&apos;</span>)
        self.assertEquals(view.func, home)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_home_view_contains_link_to_topics_page</span><span class="hljs-params">(self)</span>:</span>
        board_topics_url = reverse(<span class="hljs-string">&apos;board_topics&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: self.board.pk})
        self.assertContains(self.response, <span class="hljs-string">&apos;href=&quot;{0}&quot;&apos;</span>.format(board_topics_url))
</code></pre>
<p>&#x6CE8;&#x610F;&#x5230;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x540C;&#x6837;&#x5728; <strong>HomeTests</strong> &#x4E2D;&#x6DFB;&#x52A0;&#x4E86; <strong>setUp</strong> &#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x9700;&#x8981;&#x4E00;&#x4E2A; <strong>Board</strong> &#x5B9E;&#x4F8B;&#xFF0C;&#x5E76;&#x4E14;&#x6211;&#x4EEC;&#x5C06; <strong>url</strong> &#x548C; <strong>response</strong> &#x79FB;&#x5230;&#x4E86; <strong>setUp</strong>&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x80FD;&#x5728;&#x65B0;&#x6D4B;&#x8BD5;&#x4E2D;&#x91CD;&#x7528;&#x76F8;&#x540C;&#x7684; response&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x7684;&#x65B0;&#x6D4B;&#x8BD5;&#x662F; <strong>test_home_view_contains_link_to_topics_page</strong>&#x3002;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <strong>assertContains</strong> &#x65B9;&#x6CD5;&#x6765;&#x6D4B;&#x8BD5; response &#x4E3B;&#x4F53;&#x90E8;&#x5206;&#x662F;&#x5426;&#x5305;&#x542B;&#x7ED9;&#x5B9A;&#x7684;&#x6587;&#x672C;&#x3002;&#x6211;&#x4EEC;&#x5728;&#x6D4B;&#x8BD5;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x6587;&#x672C;&#x662F; <code>a</code> &#x6807;&#x7B7E;&#x7684; <code>href</code> &#x90E8;&#x5206;&#x3002;&#x6240;&#x4EE5;&#x57FA;&#x672C;&#x4E0A;&#x6211;&#x4EEC;&#x662F;&#x5728;&#x6D4B;&#x8BD5; response &#x4E3B;&#x4F53;&#x662F;&#x5426;&#x5305;&#x542B;&#x6587;&#x672C; <code>href=&quot;/boards/1/&quot;</code>&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x8FD0;&#x884C;&#x8FD9;&#x4E2A;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
....F.
======================================================================
FAIL: test_home_view_contains_link_to_topics_page (boards.tests.HomeTests)
----------------------------------------------------------------------
# ...

AssertionError: False is not true : Couldn&apos;t find &apos;href=&quot;/boards/1/&quot;&apos; in response

----------------------------------------------------------------------
Ran 6 tests in 0.034s

FAILED (failures=1)
Destroying test database for alias &apos;default&apos;...
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7F16;&#x5199;&#x80FD;&#x901A;&#x8FC7;&#x8FD9;&#x4E2A;&#x6D4B;&#x8BD5;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x7F16;&#x5199; <strong>home.html</strong> &#x6A21;&#x677F;&#xFF1A;</p>
<p><strong>templates/home.html</strong>
{% raw %}</p>
<pre><code class="lang-html"><span class="hljs-comment">&lt;!-- code suppressed for brevity --&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>
  {% for board in boards %}
    <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;board_topics&apos; board.pk %}&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">small</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;text-muted d-block&quot;</span>&gt;</span>{{ board.description }}<span class="hljs-tag">&lt;/<span class="hljs-name">small</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;align-middle&quot;</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;align-middle&quot;</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
  {% endfor %}
<span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>
<span class="hljs-comment">&lt;!-- code suppressed for brevity --&gt;</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EA;&#x6539;&#x52A8;&#x4E86;&#x8FD9;&#x4E00;&#x884C;&#xFF1A;</p>
<pre><code class="lang-python">{{ board.name }}
</code></pre>
<p>&#x53D8;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;board_topics&apos; board.pk %}&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
<p>&#x59CB;&#x7EC8;&#x4F7F;&#x7528; <code>{% url %}</code> &#x6A21;&#x677F;&#x6807;&#x7B7E;&#x53BB;&#x5199;&#x5E94;&#x7528;&#x7684; URL&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F; URL &#x7684;&#x540D;&#x5B57;(&#x5B9A;&#x4E49;&#x5728; URLconf&#xFF0C; &#x5373; <strong>urls.py</strong>)&#xFF0C;&#x7136;&#x540E;&#x4F60;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x9700;&#x6C42;&#x4F20;&#x9012;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5982;&#x679C;&#x662F;&#x4E00;&#x4E2A;&#x50CF;&#x4E3B;&#x9875;&#x8FD9;&#x79CD;&#x7B80;&#x5355;&#x7684; URL, &#x90A3;&#x5C31;&#x662F; <code>{% url &apos;home&apos; %}</code>&#x3002;
&#x4FDD;&#x5B58;&#x6587;&#x4EF6;&#x7136;&#x540E;&#x518D;&#x8FD0;&#x884C;&#x4E00;&#x4E0B;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
......
----------------------------------------------------------------------
Ran 6 tests in 0.037s

OK
Destroying test database for alias &apos;default&apos;...
</code></pre>
<p>&#x5F88;&#x68D2;&#xFF01;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5B83;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x662F;&#x4EC0;&#x4E48;&#x6837;&#x5B50;&#x3002;</p>
<p><img src="statics/3-9.png" alt="3-9.png"></p>
<p>&#x73B0;&#x5728;&#x8F6E;&#x5230;&#x8FD4;&#x56DE;&#x7684;&#x94FE;&#x63A5;&#x4E86;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5148;&#x5199;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<p><strong>boards/tests.py</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BoardTopicsTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-comment"># code suppressed for brevity...</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_board_topics_view_contains_link_back_to_homepage</span><span class="hljs-params">(self)</span>:</span>
        board_topics_url = reverse(<span class="hljs-string">&apos;board_topics&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.get(board_topics_url)
        homepage_url = reverse(<span class="hljs-string">&apos;home&apos;</span>)
        self.assertContains(response, <span class="hljs-string">&apos;href=&quot;{0}&quot;&apos;</span>.format(homepage_url))
</code></pre>
<p>&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
.F.....
======================================================================
FAIL: test_board_topics_view_contains_link_back_to_homepage (boards.tests.BoardTopicsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
# ...

AssertionError: False is not true : Couldn&apos;t find &apos;href=&quot;/&quot;&apos; in response

----------------------------------------------------------------------
Ran 7 tests in 0.054s

FAILED (failures=1)
Destroying test database for alias &apos;default&apos;...
</code></pre>
<p>&#x66F4;&#x65B0;&#x4E3B;&#x9898;&#x5217;&#x8868;&#x6A21;&#x7248;&#xFF1A;</p>
<p><strong>templates/topics.html</strong></p>
<pre><code class="lang-html">{% load static %}<span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><span class="hljs-comment">&lt;!-- code suppressed for brevity --&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ol</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb my-4&quot;</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">ol</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
.......
----------------------------------------------------------------------
Ran 7 tests in 0.061s

OK
Destroying test database for alias &apos;default&apos;...
</code></pre>
<p><img src="statics/3-10.png" alt="3-10.png"></p>
<p>&#x5C31;&#x5982;&#x6211;&#x4E4B;&#x524D;&#x6240;&#x8BF4;&#x7684;&#xFF0C; URL &#x8DEF;&#x7531;&#x662F;&#x4E00;&#x4E2A; web &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x57FA;&#x672C;&#x7EC4;&#x6210;&#x90E8;&#x5206;&#x3002;&#x6709;&#x4E86;&#x8FD9;&#x4E9B;&#x77E5;&#x8BC6;&#xFF0C;&#x6211;&#x4EEC;&#x624D;&#x80FD;&#x7EE7;&#x7EED;&#x5F00;&#x53D1;&#x3002;&#x4E0B;&#x4E00;&#x6B65;&#x662F;&#x5B8C;&#x6210; URL &#x7684;&#x90E8;&#x5206;&#xFF0C;&#x4F60;&#x4F1A;&#x770B;&#x5230;&#x4E00;&#x4E9B;&#x4F7F;&#x7528; URL patterns &#x7684;&#x603B;&#x7ED3;&#x3002;</p>
<h3 id="&#x5B9E;&#x7528;url&#x6A21;&#x5F0F;&#x5217;&#x8868;">&#x5B9E;&#x7528;URL&#x6A21;&#x5F0F;&#x5217;&#x8868;</h3>
<p>&#x6280;&#x5DE7;&#x90E8;&#x5206;&#x662F;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x3002;&#x6211;&#x51C6;&#x5907;&#x4E86;&#x4E00;&#x4E2A;&#x6700;&#x5E38;&#x7528;&#x7684; URL patterns &#x7684;&#x5217;&#x8868;&#x3002;&#x5F53;&#x4F60;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x7279;&#x5B9A;&#x7684; URL &#x65F6;&#x4F60;&#x53EF;&#x4EE5;&#x53C2;&#x8003;&#x8FD9;&#x4E2A;&#x5217;&#x8868;&#x3002;</p>
<table>
<thead>
<tr>
<th><strong>&#x4E3B;&#x952E; &#x81EA;&#x589E;&#x5B57;&#x6BB5;</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</td>
<td style="text-align:center"><code>(?P&lt;pk&gt;\d+)</code></td>
</tr>
<tr>
<td>&#x4E3E;&#x4F8B;</td>
<td style="text-align:center"><code>url(r&apos;^questions/(?P&lt;pk&gt;\d+)/$&apos;, views.question, name=&apos;question&apos;)</code></td>
</tr>
<tr>
<td>&#x6709;&#x6548; URL</td>
<td style="text-align:center"><code>/questions/934/</code></td>
</tr>
<tr>
<td>&#x6355;&#x83B7;&#x6570;&#x636E;</td>
<td style="text-align:center"><code>{&apos;pk&apos;: &apos;934&apos;}</code></td>
</tr>
</tbody>
</table>
<hr>
<table>
<thead>
<tr>
<th><strong>Slug &#x5B57;&#x6BB5;</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</td>
<td style="text-align:center"><code>(?P&lt;slug&gt;[-\w]+)</code></td>
</tr>
<tr>
<td>&#x4E3E;&#x4F8B;</td>
<td style="text-align:center"><code>url(r&apos;^posts/(?P&lt;slug&gt;[-\w]+)/$&apos;, views.post, name=&apos;post&apos;)</code></td>
</tr>
<tr>
<td>&#x6709;&#x6548; URL</td>
<td style="text-align:center"><code>/posts/hello-world/</code></td>
</tr>
<tr>
<td>&#x6355;&#x83B7;&#x6570;&#x636E;</td>
<td style="text-align:center"><code>{&apos;slug&apos;: &apos;hello-world&apos;}</code></td>
</tr>
</tbody>
</table>
<hr>
<table>
<thead>
<tr>
<th><strong>&#x6709;&#x4E3B;&#x952E;&#x7684; Slug &#x5B57;&#x6BB5;</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</td>
<td style="text-align:center"><code>(?P&lt;slug&gt;[-\w]+)-(?P&lt;pk&gt;\d+)</code></td>
</tr>
<tr>
<td>&#x4E3E;&#x4F8B;</td>
<td style="text-align:center"><code>url(r&apos;^blog/(?P&lt;slug&gt;[-\w]+)-(?P&lt;pk&gt;\d+)/$&apos;, views.blog_post, name=&apos;blog_post&apos;)</code></td>
</tr>
<tr>
<td>&#x6709;&#x6548; URL</td>
<td style="text-align:center"><code>/blog/hello-world-159/</code></td>
</tr>
<tr>
<td>&#x6355;&#x83B7;&#x6570;&#x636E;</td>
<td style="text-align:center"><code>{&apos;slug&apos;: &apos;hello-world&apos;, &apos;pk&apos;: &apos;159&apos;}</code></td>
</tr>
</tbody>
</table>
<hr>
<table>
<thead>
<tr>
<th><strong>Django &#x7528;&#x6237;&#x540D;</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</td>
<td style="text-align:center"><code>(?P&lt;username&gt;[\w.@+-]+)</code></td>
</tr>
<tr>
<td>&#x4E3E;&#x4F8B;</td>
<td style="text-align:center"><code>url(r&apos;^profile/(?P&lt;username&gt;[\w.@+-]+)/$&apos;, views.user_profile, name=&apos;user_profile&apos;)</code></td>
</tr>
<tr>
<td>&#x6709;&#x6548; URL</td>
<td style="text-align:center"><code>/profile/vitorfs/</code></td>
</tr>
<tr>
<td>&#x6355;&#x83B7;&#x6570;&#x636E;</td>
<td style="text-align:center"><code>{&apos;username&apos;: &apos;vitorfs&apos;}</code></td>
</tr>
</tbody>
</table>
<hr>
<table>
<thead>
<tr>
<th><strong>Year</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</td>
<td style="text-align:center"><code>(?P&lt;year&gt;[0-9]{4})</code></td>
</tr>
<tr>
<td>&#x4E3E;&#x4F8B;</td>
<td style="text-align:center"><code>url(r&apos;^articles/(?P&lt;year&gt;[0-9]{4})/$&apos;, views.year_archive, name=&apos;year&apos;)</code></td>
</tr>
<tr>
<td>&#x6709;&#x6548; URL</td>
<td style="text-align:center"><code>/articles/2016/</code></td>
</tr>
<tr>
<td>&#x6355;&#x83B7;&#x6570;&#x636E;</td>
<td style="text-align:center"><code>{&apos;year&apos;: &apos;2016&apos;}</code></td>
</tr>
</tbody>
</table>
<hr>
<table>
<thead>
<tr>
<th><strong>Year / Month</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</td>
<td style="text-align:center"><code>(?P&lt;year&gt;[0-9]{4})/(?P&lt;month&gt;[0-9]{2})</code></td>
</tr>
<tr>
<td>&#x4E3E;&#x4F8B;</td>
<td style="text-align:center"><code>url(r&apos;^articles/(?P&lt;year&gt;[0-9]{4})/(?P&lt;month&gt;[0-9]{2})/$&apos;, views.month_archive, name=&apos;month&apos;)</code></td>
</tr>
<tr>
<td>&#x6709;&#x6548; URL</td>
<td style="text-align:center"><code>/articles/2016/01/</code></td>
</tr>
<tr>
<td>&#x6355;&#x83B7;&#x6570;&#x636E;</td>
<td style="text-align:center"><code>{&apos;year&apos;: &apos;2016&apos;, &apos;month&apos;: &apos;01&apos;}</code></td>
</tr>
</tbody>
</table>
<hr>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;&#x4E2D;&#x770B;&#x5230;&#x66F4;&#x591A;&#x5173;&#x4E8E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x7EC6;&#x8282;&#xFF1A;<a href="https://simpleisbetterthancomplex.com/references/2016/10/10/url-patterns.html" target="_blank">List of Useful URL Patterns</a>&#x3002;</p>
<p>{% endraw %}</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Fundamentals-4.html" class="navigation navigation-prev " aria-label="Previous page: Django入门与实践-第10章：Django Admin 介绍">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="AdvancedConcept2.html" class="navigation navigation-next " aria-label="Next page: Django入门与实践-第12章：复用模板">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Django入门与实践-第11章：URL 分发","level":"1.12","depth":1,"next":{"title":"Django入门与实践-第12章：复用模板","level":"1.13","depth":1,"path":"AdvancedConcept2.md","ref":"./AdvancedConcept2.md","articles":[]},"previous":{"title":"Django入门与实践-第10章：Django Admin 介绍","level":"1.11","depth":1,"path":"Fundamentals-4.md","ref":"./Fundamentals-4.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["include-codeblock","livereload"],"pluginsConfig":{"include-codeblock":{"check":false,"edit":false,"fixlang":false,"lang":"","template":"default","theme":"chrome","unindent":false},"livereload":{},"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"fontsettings":{"family":"sans","size":2,"theme":"white"},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"file":{"path":"AdvancedConcepts.md","mtime":"2018-05-13T13:48:46.852Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-05-13T12:52:49.928Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

